//引入express模块
const express = require("express");
//引入连接池pool
const pool = require("../pool");
//引入路由器模块
const r = express.Router();

//用户登录
r.get("/login",(req,res)=>{
  let uname = req.query.uname;
  let upwd = req.query.upwd;
  var sql = "select uname,upwd from ub_user where uname=? and upwd=?"
  pool.query(sql,[uname,upwd],(err,results)=>{
    if(err) throw err;
    if(results.length ==0){
      res.send({msg:"login failed",code:201});
    }else{
      res.send({msg:"OK!",code:200,result:results[0]})
    }
  })
});
//验证用户名是否重复
r.get("/regUname",(req,res)=>{
  let uname = req.query.uname;
  let sql = "select * from ub_user where uname=?"
  pool.query(sql,[uname],(err,result)=>{
    if(err) throw err;
    if(result.length>0){
      res.send({code:201,msg:"用户名存在"})
    }else{
      res.send({code:200,msg:"用户名可用"})
    }
  })
})
//验证手机号是否重复
r.get("/regPhone",(req,res)=>{
  let phone = req.query.phone;
  let sql = "select * from ub_user where phone=?"
  pool.query(sql,[phone],(err,result)=>{
    if(err) throw err;
    if(result.length>0){
      res.send({code:201,msg:"手机号存在"})
    }else{
      res.send({code:200,msg:"手机号可用"})
    }
  })
})
//用户注册
r.post("/register",(req,res)=>{
  let uname = req.body.uname;
  let phone = req.body.phone;
  let upwd = req.body.upwd;
  let sql = "insert into ub_user values(null,?,?,?);"
  pool.query(sql,[uname,phone,upwd],(err,result)=>{
    if(err) throw err;
    if(result.affectedRows>0){
      res.send({code:200,msg:"注册成功"})
    }else{
      res.send({code:201,msg:"注册失败"})
    }
  })
})


//轮播图
r.get("/carousel",(req,res)=>{
  let sql = "select * from ub_slideshow limit 0,5"
  pool.query(sql,(err,result)=>{
    if(err) throw err;
    res.send(result)
  })
})

//商品列表(全部商品)
r.get("/product",(req,res)=>{
  let sql = "select * from ub_product"
  pool.query(sql,(err,result)=>{
    if(err) throw err;
    res.send(result)
  })
})

//商品列表品牌分类查询+分页
r.get("/lista",(req,res)=>{
  let classi = req.query.classi;
  let page = req.query.i;
  let pno = (page-1)*11; 
  pool.query('select * from ub_product where brand = ? limit ?,11',[classi,pno],(err,result)=>{
    if(err) throw err;
    res.send(result)
  })
})

//商品详情
r.get("/details",(req,res)=>{
  let pid = req.query.pid;
  let sql = "select * from ub_product where pid=?"
  pool.query(sql,[pid],(err,result)=>{
    if(err) throw err;
    res.send(result);
  })
})

//搜索商品
r.get("/search",(req,res)=>{
  let keyword = '%'+req.query.keyword+'%';
  let listall = 0;
  let page =  req.query.i;
  let pno =  (page-1)*11; 
  let pagecount= 0
  pool.query("select count(pid) as count from ub_product where title1 like ?",[keyword],(err,result)=>{
    if(err) throw err;
    listall = result[0].count;
    // console.log("pno:"+pno)
    // console.log("搜索数量: "+listall)
    pagecount= Math.ceil(listall/11)
  //  console.log("搜索页码: "+pagecount)
      pool.query('select * from ub_product where title1 like ? limit ?,11',[keyword,pno],(err,result)=>{
      if(err) throw err;
      res.send({result:result,pagecount:pagecount})
    })
  })
})

//商品列表大类分类查询+分页
r.get("/list",(req,res)=>{
  let classi = req.query.classi;
  let listall = 0;
  let page =  req.query.i;
  let pno =  (page-1)*11; 
  let pagecount= 0
  pool.query("select count(pid) as count from ub_product where classi = ?",[classi],(err,result)=>{
    // console.log("分类的pno:"+ pno)
    if(err) throw err;
    listall = result[0].count;
    // console.log("list: "+listall)
    pagecount= Math.ceil(listall/11)
  //  console.log("pageCount:"+pagecount)
    pool.query('select * from ub_product where classi = ? limit ?,11',[classi,pno],(err,result)=>{
      if(err) throw err;
      res.send({result:result,pagecount:pagecount})
    })
  })
})

//多级分类分页接口
r.get("/classlist",(req,res)=>{
  let classi = req.query.classi;
  let brand = req.query.brand;
  let page =  req.query.i;
  let pno =  (page-1)*11; 
  if(classi){
    var sql=`select * from ub_product where classi='${classi}' limit ${pno},11`;
  }else if(brand){
    var sql=`select * from ub_product where brand='${brand}' limit ${pno},11`;
  }
  pool.query(sql,(err,result)=>{
    if(err) throw err
      res.send(result)
  })
})





module.exports = r;